package kr.co.sdworld.webclient.spring.controller;

import kr.co.sdworld.api.apns.model.ApnsApp;
import kr.co.sdworld.api.apns.model.ApnsLogin;
import kr.co.sdworld.api.apns.model.ApnsMember;
import kr.co.sdworld.api.apns.service.AppService;
import kr.co.sdworld.api.apns.service.MemberService;
import kr.co.sdworld.api.apns.service.TokenService;
import kr.co.sdworld.webs.ServiceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CT_PropertySet 
{
	@Autowired
	private TokenService tokenService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private AppService appService;
	
	@RequestMapping(value="/propertySet/deviceTokenSave.sd", method=RequestMethod.POST)
	public ModelAndView deviceTokenSave()
	{
		String userId = ServiceContext.getParameter("userId");
		String userPw = ServiceContext.getParameter("userPw");
		String appId = ServiceContext.getParameter("appId");
		String deviceToken = ServiceContext.getParameter("deviceToken");
		
		tokenService.saveToken(appId, deviceToken, userId, userPw);
		
		ModelAndView mnv = new ModelAndView("jsonView");
		mnv.addObject("result", "success");
		
		return mnv;
	}
	
	@RequestMapping(value="/propertySet/deviceTokenSampleSave.sd", method=RequestMethod.POST)
	public ModelAndView deviceTokenSampleSave()
	{
		String userId = ServiceContext.getParameter("userId");
		String userPw = ServiceContext.getParameter("userPw");
		String appId = ServiceContext.getParameter("appId");
		String deviceToken = ServiceContext.getParameter("deviceToken");
		String deviceTokenAlias = ServiceContext.getParameter("deviceTokenAlias");
		
		tokenService.saveTokenSample(appId, deviceToken, deviceTokenAlias, userId, userPw);
		
		ModelAndView mnv = new ModelAndView("jsonView");
		mnv.addObject("result", "success");
		
		return mnv;
	}
	
	@RequestMapping(value="/propertySet/deviceTokenSampleSaveWithSession.sd", method=RequestMethod.POST)
	public ModelAndView deviceTokenSampleSaveWithSession()
	{
		ApnsLogin login = (ApnsLogin)ServiceContext.getSession().getAttribute("login");
		String userId = login.getUserId();
		String userPw = login.getUserPw();
		String appId = ServiceContext.getParameter("appId");
		String deviceToken = ServiceContext.getParameter("deviceToken");
		String deviceTokenAlias = ServiceContext.getParameter("deviceTokenAlias");
		
		tokenService.saveTokenSample(appId, deviceToken, deviceTokenAlias, userId, userPw);
		
		ModelAndView mnv = new ModelAndView("jsonView");
		mnv.addObject("result", "success");
		
		return mnv;
	}
	
	@RequestMapping(value="/propertySet/appSave.sd")
	public ModelAndView appSave() {
		String appId = ServiceContext.getParameter("appId");
		String appName = ServiceContext.getParameter("appName");
		String appDesc = ServiceContext.getParameter("appDesc");
		
		ApnsApp app = appService.saveApp(appId, appName, appDesc);
		ModelAndView mnv = new ModelAndView("jsonView");
		if(app != null) 
		{
			mnv.addObject("result", "success");
		}
		else
		{
			mnv.addObject("result", "fail");
		}
		return mnv;
	}
	
	@RequestMapping(value="/propertySet/memberSave.sd")
	public ModelAndView memberSave() {
		String userId = ServiceContext.getParameter("userId");
		String userPw = ServiceContext.getParameter("userPw");
		String userName = ServiceContext.getParameter("userName");
		String userEmail = ServiceContext.getParameter("userEmail");
		
		ApnsMember member = new ApnsMember();
		member.setUserId(userId);
		member.setUserName(userName);
		member.setUserEmail(userEmail);
		
		ApnsLogin login = new ApnsLogin();
		login.setUserId(userId);
		login.setUserPw(userPw);
		
		memberService.memberSave(member, login);
		
		ModelAndView mnv = new ModelAndView("jsonView");
		mnv.addObject("result", "success");
		
		return mnv;
	}
}
